#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2020/9/3 9:36
# @Author : wn 
# @File: pageSearch.py
# @SoftWare: PyCharm
# @Site:
# @Table:
# ｡◕‿◕｡｡◕‿◕｡｡◕‿◕｡
from operator import itemgetter

from django.forms import model_to_dict
from django.http import JsonResponse

from Manager.models import *
from Manager.utils.model_choices import *


def search_ordersearch(Tradingday=None, RuleID=None, OrderInnerID=None, AccountID=None, InstrumentID=None, OrderStatus=None):
    '''

    查询条件
	交易日：Tradingday（格式YYYYMMDD），必填，默认填写当前日期
	策略编号: RuleID
	报单内部编号：OrderInnerID
	交易账号：AccountID
	合约代码：InstrumentID
	挂单状态： OrderStatus（展示枚举值）

    '''
    search_obj = Ruleorder.objects.all()
    search_list = search_obj.values()
    # 搜索条件 Tradingday
    if Tradingday != None:
        if Tradingday != '':
            search_obj = search_obj.filter(tradingday=Tradingday)
            search_list = search_obj.values()

    # 搜索条件 OrderInnerID
    if OrderInnerID != None:
        if OrderInnerID != '':
            search_obj = search_obj.filter(orderinnerid__contains=OrderInnerID)
            search_list = search_obj.values()

    # 搜索条件 AccountID
    if AccountID != None:
        if AccountID != '':
            search_obj = search_obj.filter(accountid__contains=AccountID)
            search_list = search_obj.values()

    # 搜索条件 InstrumentID
    if InstrumentID != None:
        if InstrumentID != '':
            search_obj = search_obj.filter(instrumentid__contains=InstrumentID)
            search_list = search_obj.values()

    # 搜索条件 OrderStatus
    if OrderStatus != None:
        if OrderStatus != '':
            search_obj = search_obj.filter(orderstatus=OrderStatus)
            search_list = search_obj.values()

    # 搜索条件 RuleID
    if RuleID != None:
        if RuleID != '':
            # 数据库里存的是int
            temp_list = []
            for item in search_obj:
                if RuleID in str(item.ruleid):
                    item_dict = model_to_dict(item)
                    temp_list.append(item_dict)
            search_list = temp_list

    for item in search_list:
        item['quotetype'] = QuoteTypeChoices.get(item['quotetype'], None)
        item['orderstatus'] = OrderStatusChoices.get(item['orderstatus'], None)
        item['offsetflag'] = OffsetFlagChoices.get(item['offsetflag'], None)
        item['direction'] = DirectionChoices.get(item['direction'], None)
        item['commoditytype'] = CommodityTypeChoices.get(item['commoditytype'], None)
        if (item.get('tradingday') is None):
            item['tradingday'] = '-1'
    # 排序
    result_list_sorted = sorted(search_list, key=itemgetter('tradingday'), reverse=True)
    return JsonResponse(result_list_sorted, safe=False)


def search_positionsearch(Tradingday=None, CombiNo=None, InstrumentID=None, PosiDirection=None):
    search_obj = Ruleposition.objects.all()
    search_list = search_obj.values()
    # 搜索条件 Tradingday
    if Tradingday != None:
        if Tradingday != '':
            search_obj = search_obj.filter(tradingday=Tradingday)
            search_list = search_obj.values()

    # 搜索条件 CombiNo
    if CombiNo != None:
        if CombiNo != '':
            search_obj = search_obj.filter(combino__contains=CombiNo)
            search_list = search_obj.values()

    # 搜索条件 InstrumentID
    if InstrumentID != None:
        if InstrumentID != '':
            search_obj = search_obj.filter(instrumentid__contains=InstrumentID)
            search_list = search_obj.values()

    # 搜索条件 PosiDirection
    if PosiDirection != None:
        if PosiDirection != '':
            search_obj = search_obj.filter(posidirection=PosiDirection)
            search_list = search_obj.values()

    for item in search_list:
        item['posidirection'] = PosiDirectionChoices.get(item['posidirection'], None)
        item['hedgeflag'] = HedgeFlagChoices.get(item['hedgeflag'], None)
        if (item.get('tradingday') is None):
            item['tradingday'] = '-1'
    # 排序
    result_list_sorted = sorted(search_list, key=itemgetter('tradingday'), reverse=True)
    return JsonResponse(result_list_sorted, safe=False)


def search_tradesearch(Tradingday=None, RuleID=None, TradeID=None, OrderSysID=None, AccountID=None, InstrumentID=None):
    search_obj = Ruletrade.objects.all()
    search_list = search_obj.values()
    # 搜索条件 Tradingday
    if Tradingday != None:
        if Tradingday != '':
            search_obj = search_obj.filter(tradingday=Tradingday)
            search_list = search_obj.values()

    # 搜索条件 OrderSysID
    if OrderSysID != None:
        if OrderSysID != '':
            search_obj = search_obj.filter(ordersysid__icontains=OrderSysID)
            search_list = search_obj.values()

    # 搜索条件 AccountID
    if AccountID != None:
        if AccountID != '':
            search_obj = search_obj.filter(accountid__contains=AccountID)
            search_list = search_obj.values()

    # 搜索条件 InstrumentID
    if InstrumentID != None:
        if InstrumentID != '':
            search_obj = search_obj.filter(instrumentid__contains=InstrumentID)
            search_list = search_obj.values()

    # 搜索条件 TradeID
    if TradeID != None:
        if TradeID != '':
            search_obj = search_obj.filter(tradeid__contains=TradeID)
            search_list = search_obj.values()

    # 搜索条件 RuleID
    if RuleID != None:
        if RuleID != '':
            # 数据库里存的是int
            temp_list = []
            for item in search_obj:
                if RuleID in str(item.ruleid):
                    item_dict = model_to_dict(item)
                    temp_list.append(item_dict)
            search_list = temp_list

    for item in search_list:
        item['offsetflag'] = OffsetFlagChoices.get(item['offsetflag'], None)
        item['direction'] = DirectionChoices.get(item['direction'], None)
        if (item.get('tradingday') is None):
            item['tradingday'] = '-1'
    # 排序
    result_list_sorted = sorted(search_list, key=itemgetter('tradingday'), reverse=True)
    return JsonResponse(result_list_sorted, safe=False)


def search_selftradesearch():
    search_obj = Selftradechannel.objects.all()
    search_list = search_obj.values()
    for item in search_list:
        # 操作，在item的基础上，新加一个字段哦
        item['selftradeoption'] = item['channelid']
        if (item.get('channelid') is None):
            item['channelid'] = '-1'
    # 排序
    result_list_sorted = sorted(search_list, key=itemgetter('channelid'), reverse=True)
    return JsonResponse(result_list_sorted, safe=False)


def search_accountmapping(combino=None, combiname=None, accid=None, accname=None):
    search_obj = Accountmapping.objects.all()
    search_list = search_obj.values()
    # 搜索条件 combino
    if combino != None:
        if combino != '':
            search_obj = search_obj.filter(combino__icontains=combino)
            search_list = search_obj.values()

    # 搜索条件 combiname
    if combiname != None:
        if combiname != '':
            search_obj = search_obj.filter(combiname__icontains=combiname)
            search_list = search_obj.values()

    # 搜索条件 accid
    if accid != None:
        if accid != '':
            search_obj = search_obj.filter(accid__contains=accid)
            search_list = search_obj.values()

    # 搜索条件 accname
    if accname != None:
        if accname != '':
            search_obj = search_obj.filter(accname__contains=accname)
            search_list = search_obj.values()

    for item in search_list:
        # 操作，在item的基础上，新加一个字段哦
        item['amoption'] = item['id']
        if (item.get('combino') is None):
            item['combino'] = '-1'
    # 排序
    result_list_sorted = sorted(search_list, key=itemgetter('combino'), reverse=True)
    return JsonResponse(result_list_sorted, safe=False)
